﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EEGWin.model;
using EEGWin.dao;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using ZOpenGLControl;
using ZUtil;

namespace EEGWin
{
    public partial class LookDetailFrame : CCWin.CCSkinMain
    {
        private UserDao userDao = new UserDao();
        private ScanDataDao scanDataDao = new ScanDataDao();
        public static void showDialog(int id)
        {

            new LookDetailFrame(id).ShowDialog();
        }

        private int id;
        public LookDetailFrame(int id)
        {
            this.id = id;
            InitializeComponent();
        }

        private void LookDetailFrame_Load(object sender, EventArgs e)
        {
            loadConfig();


            UserModel user = userDao.queryById(id);
            if (user == null)
            {
                MessageBox.Show("该用户不存在或已被删除");
                this.Close();
                return;
            }

            this.Text = user.name;

            ScanDataModel scanDataModel = scanDataDao.queryByUserId(user.id);
            if (scanDataModel == null)
            {
                MessageBox.Show("该记录不存在或已被删除");
                this.Close();
                return;
            }

            var dataItems = (List<DataItem>)deserialize(scanDataModel.data);

            lineControl1.load(dataItems);

        }

        private void loadConfig()
        {
            int color_line1 = int.Parse(ConfigUtil.readSetting("color_line1"));
            int color_line2 = int.Parse(ConfigUtil.readSetting("color_line2"));
            int color_line3 = int.Parse(ConfigUtil.readSetting("color_line3"));
            int color_line4 = int.Parse(ConfigUtil.readSetting("color_line4"));
            int color_line5 = int.Parse(ConfigUtil.readSetting("color_line5"));
            int color_line6 = int.Parse(ConfigUtil.readSetting("color_line6"));

            lineControl1.setColor(new int[] {
                color_line1,color_line2,color_line3,color_line4,color_line5,color_line6
            });

        }

        private Object deserialize(Byte[] bytes)
        {
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms);
            }
        }
    }
}
